﻿using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MJankowski.org.AutoMock.MockFactories
{
    public class MoqMockingFactory : IMockingFactory
    {
        public object CreateMock(Type dependencyType)
        {
            try
            {
                var mock = typeof(Mock<>).MakeGenericType(dependencyType).GetConstructor(Type.EmptyTypes).Invoke(new object[] { });
                return mock.GetType().GetProperties().Single(f => f.Name == "Object" && f.PropertyType == dependencyType).GetValue(mock, new object[] { });
            }
            catch (Exception ex)
            {
                const string messageFormat = "Unable to create mock for type '{0}'. Inject the dependency instance in constructor or see inner exception for more details.";
                throw new Exception(String.Format(messageFormat, dependencyType), ex);
            }
        }
    }
}
